<?php


namespace app\admin\model;


use think\Db;

class RetreatTombModel extends BaseModel
{
    protected $name='retreat_tomb';

    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $createTime = 'add_time';

    public   function doAddDate($parem=[],$customer_id){
        $approval_id=$parem['approval_id'];
        $id = $parem['id'];
        $deceased = $parem['deceased'];
        $tmxq = $parem['tmxq'];

        $this->startTrans();
        try {
            $deceased['mw_id'] = $parem['mw_id'];
            $deceased['approval_id'] = $approval_id;
            $deceased['mw_num'] = $parem['mw_num'];
            $deceased['order_id'] = $parem['order_id'];
            $deceased['customer_id'] = $customer_id;
            $deceased['add_time'] = date_to_unixtime($tmxq['add_time']);
            $deceased['reason_info'] = $tmxq['reason_info'];
            $deceased['loss_ratio'] = $tmxq['loss_ratio'];
            $deceased['refund_amount'] = $tmxq['refund_amount'];
            $deceased['refund_type'] = $tmxq['refund_type'];
            $deceased['closing_price'] = $tmxq['closing_price'];
            $deceased['is_recycle'] = $tmxq['is_recycle'];
            $deceased['handle_user'] = cmf_get_admin_name();
            $deceased['add_user'] = cmf_get_admin_name();
            $deceased['add_user'] = cmf_get_admin_name();

            if ($id) {
                $this->update($deceased, ['id' => $id]);
            } else {
                $retreat_id = $this->create($deceased);
                $id = $retreat_id['id'];
            }
            if ($parem['otype'] == 1) {
                $approval_id = ApprovalModel::addSave(2, 4, $id);
                if ($id) {
                    ApprovalLogModel::addLog($approval_id, '编辑，审核中', '申请处理中');
                } else {
                    ApprovalLogModel::addLog($approval_id, '已提交，审核中', '申请处理中');
                }

                $pay_info = OrderPayLogModel::where(['id'=>$parem['payid']])->find();
                OrderPayLogModel::update(['refund_state'=>1],['id'=>$pay_info['id']]);
//                unset($pay_info['id']);
                $refund_info['type'] = 2;
                $refund_info['other_id'] =  $id;
                $refund_info['tomb_id'] = $parem['mw_id'];
                $refund_info['pay_id'] = $parem['payid'];
                $refund_info['refund_sn'] =  PayRefundModel::getselfPayNum('add_time','refund_sn','TM');
                $refund_info['real_name'] =$pay_info['real_name'];
                $refund_info['real_phone'] =$pay_info['real_phone'];
                $refund_info['total_price'] =$pay_info['total_price'];
                $refund_info['refund_state'] = 1;
                $refund_info['refund_img'] = $deceased['pay_file'];
                $refund_info['refund_amount'] = $tmxq['refund_amount'] ;
                $refund_info['refund_type'] = $tmxq['refund_type'];
                $refund_info['refund_apply_time'] = time();
                $refund_info['refund_apply_state'] = 1;
                $refund_info['add_user'] = cmf_get_current_admin_id();
                $refund_info['add_time'] = time();
                $refund_id = PayRefundModel::doDataAdd($refund_info);
                $this->update(['refund_id'=>$refund_id,'approval_id'=>$approval_id],['id'=>$id]);

            }

//            // 发送短信
            if (isset($deceased['is_sms'])){
                //发送短信
                $iSenf = sendNoticeSms(4,$deceased['certificate_phone'],['tomb_num'=>$parem['mw_num']],$errorMsg);
                if ($iSenf==false){
                    return $this->error($errorMsg);
                }
            }
//
            $this->commit();
            return true;
        }catch(\Exception $exception){
            $this->rollback();
            $this->error='添加失败';
            return false;
        }
    }

}